package com.miplus.ccrm.core.service.iou;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.*;
import com.miplus.ccrm.core.controller.vo.action.ICmdReqVo;
import com.miplus.ccrm.core.controller.vo.action.ICmdResVo;
import com.miplus.ccrm.core.controller.vo.iou.IBillReqVo;
import com.miplus.ccrm.core.controller.vo.iou.IBillResVo;
import com.miplus.ccrm.core.pojo.IouBill;
import com.miplus.ccrm.core.mapper.IouBillMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author pc
* @description 针对表【IOU_BILL(借据表)】的数据库操作Service实现
* @createDate 2024-10-26 12:41:23
*/
@Service
public class IouBillServiceImpl extends MPJBaseServiceImpl<IouBillMapper, IouBill>
    implements IouBillService{

    @Resource
    private IouCommand iouCommand;

    @Override
    public IPage<IBillResVo> caseList(JSysUser user, IBillReqVo paras)
    {
        String $usrid=user.getUsrid();
        if(JPubUtils.IsEmpty(paras.getContractnumber())&&JPubUtils.IsEmpty(paras.getCollectno())){
            throw new ServiceException(EnumRCode.ArgErr.value, EnumRCode.ArgErr.name);
        }
        String cardstar= JUsrPerm.getPermission(user,JUsrPerm.DISSTAR_CARD);
        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                IBillResVo.class,
                new MPJLambdaWrapper<IouBill>()
                        .selectAs("fun_getnames('COLLECT.BILL.STATUS',t.status)", IBillResVo::getStatusdsc)
                        .selectAs("fun_getnames('FI.COLLECT.CHANNEL',t.CHANNEL_NO)", IBillResVo::getChannelNodsc)
                        .selectAs("fun_getnames('BUSINESS.TYPE',t.loan_type)", IBillResVo::getLondTypedsc)
                        .selectAs("fun_getnames('BUSINESS.SUB.TYPE',t.loan_type_sub)", IBillResVo::getLondTypeSubdsc)
                        .selectAs(String.format("fi_starSecu('%s','%s',t.repay_card,'%s')", $usrid, JUsrPerm.DISSTAR_CARD, cardstar), IBillResVo::getRepayCardS)
                        .selectAs("fun_getnames('CREDIT.ACCT.FLEVEL',t.flevel)", IBillResVo::getFleveldsc)
                        .selectAs("fun_getnames('CREDIT.ACCT.FLEVEL',t.pre_flevel)", IBillResVo::getPreFleveldsc)
                        .selectAll(IouBill.class)
                        .eq(JPubUtils.IsNotEmpty(paras.getContractnumber()),IouBill::getContractnumber,paras.getContractnumber())
                        .eq(JPubUtils.IsNotEmpty(paras.getCollectno()),IouBill::getBillid,paras.getCollectno())
                        .orderByAsc(IouBill::getOverdueStartdt)
        );
    }

    @Override
    public String getRepayCardNo(String billno)
    {
        IouBill bill=baseMapper.selectById(billno);
        if(bill!=null){
            return bill.getRepayCard();
        }
        return null;
    }

    @Override
    public String getChannelCode(String billno)
    {
        IouBill bill=baseMapper.selectById(billno);
        if(bill!=null){
            return bill.getChannelNo();
        }
        return null;
    }

    @Override
    public ICmdResVo refreshPayBal(ICmdReqVo reqVo)
    {
        String billno=reqVo.getBillno();
        IouBill bill=baseMapper.selectById(billno);
        //FS:提前结清
        //NF:提前还款
        //NM:归还欠款
        return iouCommand.refPayAmount(bill.getContractnumber(),billno,"NM");
    }
}




